$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/customer/list',
        datatype: "json",
        colModel: [
            {label: '用户ID', name: 'yhbCusTerId', index: "user_id", width: 45, key: true,align:"center"},
            {label: '用户名', name: 'username', width: 75,align:"center"},
            {label: '手机号', name: 'phone', sortable: false, width: 75,align:"center"},
            {label: 'sn', name: 'sn', index: "create_time", width: 85,align:"center"},
            {label: '类别', name: 'terminalName', width: 60,align:"center"},
            {label: '型号', name: 'model', width: 90,align:"center"},
            {label: '申请时间', name: 'createDate',  width: 85,align:"center"},
            {label: '修改时间', name: 'updateDate',  width: 85,align:"center"},
            {
                label: '审批状态', name: 'state', width: 100, formatter: function ( value,options,row) {

                    return str=value == 1 ? "已通过" : value == 2 ? "审核中" : "已拒绝";
                },align:"center"
            },
            {
                label: '申请状态', name: 'isDel', width: 100, formatter: function ( value,options,row) {

                    return value == 1 ? "申请取消" :"正常";
                },align:"center"
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});
var exmiane;

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            username: null,
            phone: null,
            state: 0
        },
        showList: true,
        title: null,
        roleList: {},
        user: {
            status: 1,
            deptId: null,
            deptName: null,
            roleIdList: []
        },
        states: [
            {
                value: 0,
                msg: "请选择状态"
            },
            {
                value: 1,
                msg: "审核通过"
            },
            {
                value: 2,
                msg: "审核中"
            },
            {
                value: 3,
                msg: "已拒绝"
            },
        ],
        yhbcusterId:null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";

        },

        update: function () {
            var userId = getSelectedRow();
            if (userId == null) {
                return;
            }

            vm.showList = false;
            vm.title = "修改";

        },
        del: function () {
            var userIds = getSelectedRows();
            if (userIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/user/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        examine:function(){
            let rowid=getSelectedRow();
            if(rowid==null){
                return;
            }
            let row=$("#jqGrid").jqGrid('getRowData',rowid);

            if(row.isDel=='申请取消'){
                layer.msg("该申请已被用户取消,不可审批");
                return;
            }

            if(row.state=='已通过'){

                layer.msg("该申请已通过,请勿重复审批");
                return;
            }

            if(row.state=='已拒绝'){

                layer.msg("该申请已被拒,请勿重复审批");
                return;
            }
            vm.openWin(row.yhbCusTerId);

        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'username': vm.q.username,'phone':vm.q.phone,'state':vm.q.state},
                page: page
            }).trigger("reloadGrid");
        },
        openWin:function(yhbCusTerId){

            vm.yhbcusterId=yhbCusTerId;

           exmiane= layer.open({
                type: 1,
                title:"审核",
                content:$("#exmaine"),
                area:['380px','220px'],
                btn:['关闭'],
                btn1:function (index) {
                    layer.close(index)
                }


            })
        },
        verfy:function (state) {
                $.ajax({
                    url:baseURL+"sys/customer/exmaine",
                    datatype: "post",
                    data:{'yhbCusTerId':vm.yhbcusterId,'state':state},
                    success:function (r) {
                       if(r.code==0){
                           alert('审批成功');
                       }else {
                           alert(r.msg)
                       }
                       vm.reload();
                    }

                })
            layer.close(exmiane);
        }
    }
});